package com.baseweb.webadmin.core.util;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static Date getDate(String timeStr, String reg) {
        SimpleDateFormat format = new SimpleDateFormat(reg);
        Date date = null;
        try {
            date = format.parse(timeStr);
        } catch (ParseException e) {
        }
        return date;
    }

    public static Date getDate(String timeStr) {
        return getDate(timeStr, "yyyy-MM-dd");
    }

    /**
     * 获得昨天的时间 例如 2011-7-26
     *
     * @return
     */
    public static String getYesterday() {
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DAY_OF_YEAR, -1);
        Date date2 = ca.getTime();
        String yesterday = DateFormat.getDateTimeInstance().format(date2);
        yesterday = yesterday.split(" ")[0];
        return yesterday;
    }

    public static DateTime getDateTimeDetail(String dateName) {
        String reg = "yyyy-MM-dd HH:mm:ss";
        return getDateTime(dateName, reg);
    }

    public static DateTime getDateTime(String dateName) {
        String reg = "yyyy-MM-dd";
        return getDateTime(dateName, reg);
    }

    public static DateTime getDateTime(String dateName, String reg) {
        DateTimeFormatter dateTimeFormatter = org.joda.time.format.DateTimeFormat.forPattern(reg);
        return dateTimeFormatter.parseDateTime(dateName);
    }

    public static DateTime getMonth(String date) {
        if (date == null)
            return null;
        else {
            String[] dates = date.split("-");
            int year = Integer.parseInt(dates[0]);
            int month = Integer.parseInt(dates[1]);
            DateTime oldDateTime = new DateTime(year, month, 1, 0, 0, 0, 0);
            DateTime startDateTime = oldDateTime.minusMonths(1);
            startDateTime = startDateTime.dayOfMonth().withMaximumValue();

            DateTime endDateTime = oldDateTime.plusMonths(1);
            endDateTime = endDateTime.dayOfMonth().withMinimumValue();

            System.out.println(startDateTime);
            System.out.println(endDateTime);

            return null;
        }

    }

    public static Boolean checkDate(String date) {
        try {
            if (StringUtils.isNotBlank(date)) {
                new SimpleDateFormat("yyyy-MM-dd").parse(date);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String dateToString(DateTime date) {
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        String dateStr = df.format(date);
        return date.toString("yyyy-MM-dd");
    }

    public static String dateToString(DateTime date, String format) {
        return date.toString(format);
    }

    public static void main(String[] args) {
//        System.out.print(new DateTime(Long.parseLong("1332444564000")));
//        HandleCpsInForm form = new HandleCpsInForm();
//        HandleCpsController handleCpsController = new HandleCpsController();
//        form.setSd(Long.parseLong("1332444564"));
//        handleCpsController.handleError(form,"ss");
//        System.out.print(form.getSd());
    }
}
